package com.hspedu.homework;

/**
 * @author DL5O
 * @version 1.0
 */
public class Homework01 {
    public static void main(String[] args) {
        String str = "abcdef";
        int start = 1;
        int end = 4;

        System.out.println("源字符串："+str);
        String reverse = reverse(str, start, end);
        System.out.println("翻转后："+reverse);

        StringBuilder sb = new StringBuilder(str);
        System.out.println(sb.reverse());
    }
    //将指定范围的字符进行翻转
    //  start=1 end = 4
    //  源字符串：abcdef
    //  翻转后：aedcbf
    public static String reverse(String str, int start, int end) {

        //我的思路:
        //1.将字符串转成一个字符串数组，因为字符串不能更改元素(现在来看)
        //2.利用循环(start < end)
        //  让ch[start] 和 ch[end]交换即可
        char[] ch = str.toCharArray();
        while(start < end){
            char temp = ch[start];
            ch[start++] = ch[end];
            ch[end--] = temp;
        }

        str= new String(ch);
        return str;
    }
}
